package cn.xo68.boot.auth.server.service;

import java.util.List;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.OauthToken;
import cn.xo68.boot.auth.server.mapper.OauthTokenMapper;
import cn.xo68.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



/** 
 * OauthToken Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月26日 15:34:35
 *
 */
@CacheConfig(cacheNames = {AuthConstants.CACHE_NAME_OAUTH2_TOKEN})
@Service
public class OauthTokenService{
    @Autowired
    private OauthTokenMapper oauthTokenMapper;

    @Cacheable(key = "#token")
    public OauthToken get(String token) {
        OauthToken oauthToken= oauthTokenMapper.get(token);
        return oauthToken;
    }

    @CachePut(key = "#oauthToken.token")
    public OauthToken insert(OauthToken oauthToken) {
        oauthTokenMapper.insert(oauthToken);
        return oauthToken;
    }

    @CachePut(key = "#oauthToken.token")
    public OauthToken update(OauthToken oauthToken) {
        oauthTokenMapper.update(oauthToken);
        return oauthToken;
    }

    @CacheEvict(key = "#token")
    public void delete(String token) {
        oauthTokenMapper.delete(token);
    }
    
    public List<OauthToken> list() {
        return oauthTokenMapper.list();
    }
	
	public PageInfo<OauthToken> list(int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber, pageSize,true);
        List<OauthToken> docs = oauthTokenMapper.list();
        PageInfo<OauthToken> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }
}